<template>
  <div class="low-code-design-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>设计列表</span>
        <el-button style="float: right; padding: 3px 0" @click="add" type="text">表单设计</el-button>
      </div>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="formName" label="表单名称"> </el-table-column>
        <el-table-column prop="formCode" label="表单编码"> </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="modify(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click="editor(scope.row)">编辑</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="confirmDel(scope.row)">
              <el-button type="text" size="small" :style="{ marginLeft: '20px' }" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="ismodify?'编辑表单':'新增表单'" :visible.sync="dialogFormInfo" width="45%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="表单名称" prop="formName">
          <el-input v-model="ruleForm.formName"></el-input>
        </el-form-item>
        <el-form-item label="表单编码" prop="formCode">
          <el-input v-model="ruleForm.formCode"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormInfo = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog class="low-code-design-dialog" title="页面设计" :visible.sync="dialogVisible" :close-on-click-modal="false" fullscreen append-to-body destroy-on-close>
      <low-code v-if="dialogVisible" ref="design" :params="params" />
    </el-dialog>
  </div>
</template>

<script>
  import axios from "axios";
  import lowCode from "@/low-code/index.vue";
  //  import {cache} from "@/low-code/utils/storage";
  import { event } from "@/low-code/utils/event.js";
  import { apiurl } from "@/config/"
  export default {
    components: {
      lowCode,
    },
    data () {
      return {
        ismodify: false,
        dialogFormInfo: false,
        dialogVisible: false,
        tableData: [],
        ruleForm: {
          id: "",
          formName: "",
          formCode: "",
          formTemplate: "",
        },
        rules: {
          formName: [
            { required: true, message: "请输入表单名称", trigger: "blur" },
          ],
          formCode: [
            { required: true, message: "请输入表单编码", trigger: "blur" },
          ],
        },
        params:{
          AAA:1
        }
      };
    },
    methods: {
      add () {
        this.dialogFormInfo = true;
        this.ismodify = false
        Object.assign(this.ruleForm, {
          id: "",
          formName: "",
          formCode: "",
          formTemplate: "",
        })
      },
      modify (row) {
        this.ismodify = true
        this.dialogFormInfo = true;
        Object.assign(this.ruleForm, { ...row })
      },
      editor (row) {
        axios({
          method: "get",
          url: apiurl+"/editorFormTemplate",
          params: {
            id: row.id,
          },
        }).then((result) => {
          console.log("result", result);
          this.dialogVisible = true;
          this.$nextTick(() => {
            this.ruleForm.id = row.id;
            this.$refs["design"].importEditor(result.data.data.formTemplate);
          });
        });
      },
      confirmDel (row) {
        console.log(row);
        axios({
          method: "post",
          url: apiurl+"/deleteFormTemplate",
          data: {
            id: row.id,
          },
        }).then((result) => {
          console.log("result", result);
          this.getFormList();
        });
      },
      getFormList () {
        axios({
          method: "get",
          url: apiurl+"/formTemplateList",
        }).then((result) => {
          console.log("result", result);
          this.tableData = result.data.data;
        });
      },
      submitForm (formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            this.dialogFormInfo = false;
            if (!this.ismodify) {
              this.dialogVisible = true;
            }else{
        axios({
          method: "post",
          url: apiurl+"/modifyForm",
          data: this.ruleForm,
        }).then((result) => {
          this.getFormList();
          console.log("result", result);
        });
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
    watch: {
      dialogFormInfo (value) {
        if (!value) {
          this.ruleForm.id = ''
        }
      },
      dialogVisible(value){
        if (!value) {
          // cache.clear()
        }
      }
    },
    mounted () {
      event.on("saveTemplate", (template) => {
        this.ruleForm.formTemplate = template;
        axios({
          method: "post",
          url: apiurl+"/saveFormTemplate",
          data: this.ruleForm,
        }).then((result) => {
          console.log("result", result);
        });
      });

      this.getFormList();
    },
  };
</script>

<style  scoped>
.low-code-design-page {
  padding: 20px;
}
.low-code-design-dialog {
}
.low-code-design-dialog >>> .el-dialog__body {
  padding: 0px;
}

::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 8px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 15px;
}

::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 8px;
  -webkit-box-shadow: inset 0 0 5px #dcdfe6;
  box-shadow: inset 0 0 3px #dcdfe6;
  background: #dcdfe6;
}

::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  background: #ffffff;
}
</style>